package com.software.levelupandroid.home.ui.home;

import android.content.Context;
import android.os.Looper;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.Result;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.entity.User;
import com.software.levelupandroid.home.CallBack;

import java.util.List;

public class HomeViewModel extends ViewModel {

    //任务列表的LiveData
    private final MutableLiveData<Resource<List<Task>>> taskListLiveData;
    private final MutableLiveData<Resource<List<Task>>> toDoTaskListLiveData;
    private final MutableLiveData<Resource<Result>> isUpdateLiveData;

    public HomeViewModel() {
        taskListLiveData = new MutableLiveData<>();
        toDoTaskListLiveData = new MutableLiveData<>();
        isUpdateLiveData = new MutableLiveData<>();
    }

    public LiveData<Resource<List<Task>>> getTaskListLiveData() {
        return taskListLiveData;
    }

    public LiveData<Resource<List<Task>>> getToDoTaskListLiveData(){
        return toDoTaskListLiveData;
    }

    public MutableLiveData<Resource<Result>> getIsUpdateLiveData() {
        return isUpdateLiveData;
    }

    /**
     * 获取本日任务信息
     * @param context
     */
    public void getTaskListInfo(Context context){
        HomeRepository.getHomeRepository().getTasksFromServer(new CallBack<List<Task>>() {
            @Override
            public void onSuccess(List<Task> data) {
                runOnMainThread(taskListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                Log.e("home:", msg);
                runOnMainThread(taskListLiveData, Resource.failure(msg, null));
            }
        }, context);
    }

    /**
     * 更新本日任务信息
     * @param context
     */
    public void updateTaskListInfo(Context context){
        HomeRepository.getHomeRepository().updateTasksFromServer(new CallBack<List<Task>>() {
            @Override
            public void onSuccess(List<Task> data) {

                runOnMainThread(taskListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                runOnMainThread(taskListLiveData, Resource.failure(msg, null));
            }
        }, context);
    }

    /**
     * 获取指定日期下的所有任务
     * @param context
     */
    public void getAllTasksByDate(Context context){
        HomeRepository.getHomeRepository().getAllTasksByDate(new CallBack<List<Task>>() {
            @Override
            public void onSuccess(List<Task> data) {

                runOnMainThread(taskListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                runOnMainThread(taskListLiveData, Resource.failure(msg, null));
            }
        },context);
    }

    /**
     * 获取明日所有任务
     * @param context
     */
    public void getTomorrowTasksByDate(Context context){
        HomeRepository.getHomeRepository().getTomorrowAllTasksByDate(new CallBack<List<Task>>() {
            @Override
            public void onSuccess(List<Task> data) {

                runOnMainThread(taskListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                runOnMainThread(taskListLiveData, Resource.failure(msg, null));
            }
        },context);
    }

    /**
     * 获取指定用户的所有待办
     * @param context
     */
    public void getAllToDoTasks(Context context){
        HomeRepository.getHomeRepository().getAllToDoTasks(new CallBack<List<Task>>() {
            @Override
            public void onSuccess(List<Task> data) {
                runOnMainThread(toDoTaskListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                runOnMainThread(toDoTaskListLiveData, Resource.failure(msg, null));
            }
        }, context);
    }

    /**
     * 任务是否完成
     * @param task
     * @param context
     */
    public void isFinishTask(Task task, Context context){
        HomeRepository.getHomeRepository().isFinishTask(task, new CallBack<Task>() {
            @Override
            public void onSuccess(Task data) {
                Resource<List<Task>> resource = taskListLiveData.getValue();
                List<Task> tasks = resource.getData();
                if (tasks != null){
                    int index = tasks.indexOf(task);
                    if (index >= 0 ){
                        tasks.set(index, task);
                        resource.onSuccess(tasks);
                        runOnMainThread(taskListLiveData, resource);
                    }else {
                        resource.onFailure("任务索引小于0", tasks);
                        runOnMainThread(taskListLiveData, resource);
                    }
                }else {
                    resource.onFailure("未获取到任务", tasks);
                    runOnMainThread(taskListLiveData, resource);
                }
            }

            @Override
            public void onFailure(String msg) {
                Resource<List<Task>> resource = taskListLiveData.getValue();
                List<Task> data = resource.getData();
                resource.onFailure(msg, data);
                runOnMainThread(taskListLiveData, resource);
            }
        }, context);
    }

    /**
     * 待办是否完成
     * @param task
     * @param context
     */
    public void isFinishToDoTask(Task task, Context context){
        HomeRepository.getHomeRepository().isFinishTask(task, new CallBack<Task>() {
            @Override
            public void onSuccess(Task data) {
                Resource<List<Task>> resource = toDoTaskListLiveData.getValue();
                List<Task> tasks = resource.getData();
                if (tasks != null){
                    int index = tasks.indexOf(task);
                    if (index >= 0 ){
                        tasks.set(index, task);
                        resource.onSuccess(tasks);
                        runOnMainThread(toDoTaskListLiveData, resource);
                    }else {
                        resource.onFailure("任务索引小于0", tasks);
                        runOnMainThread(toDoTaskListLiveData, resource);
                    }
                }else {
                    resource.onFailure("未获取到任务", tasks);
                    runOnMainThread(toDoTaskListLiveData, resource);
                }
            }

            @Override
            public void onFailure(String msg) {
                Resource<List<Task>> resource = toDoTaskListLiveData.getValue();
                List<Task> data = resource.getData();
                resource.onFailure(msg, data);
                runOnMainThread(toDoTaskListLiveData, resource);
            }
        }, context);
    }

    /**
     * 删除任务
     * @param task
     * @param context
     */
    public void deleteTask(Task task, Context context){
        HomeRepository.getHomeRepository().deleteTask(task, new CallBack() {
            @Override
            public void onSuccess(Object data) {
                Resource<List<Task>> resource = taskListLiveData.getValue();
                List<Task> tasks = resource.getData();
                if (tasks != null){
                    int index = tasks.indexOf(task);
                    if (index >= 0){
                        tasks.remove(index);
                        resource.onSuccess(tasks);
                        runOnMainThread(taskListLiveData, resource);
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Resource<List<Task>> resource = taskListLiveData.getValue();
                List<Task> data = resource.getData();
                resource.onFailure(msg, data);
                runOnMainThread(taskListLiveData, resource);
            }
        }, context);
    }

    /**
     * 删除待办
     * @param task
     * @param context
     */
    public void deleteToDoTask(Task task, Context context){
        HomeRepository.getHomeRepository().deleteTask(task, new CallBack() {
            @Override
            public void onSuccess(Object data) {
                Resource<List<Task>> resource = toDoTaskListLiveData.getValue();
                List<Task> tasks = resource.getData();
                if (tasks != null){
                    int index = tasks.indexOf(task);
                    if (index >= 0){
                        tasks.remove(index);
                        resource.onSuccess(tasks);
                        runOnMainThread(toDoTaskListLiveData, resource);
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Resource<List<Task>> resource = toDoTaskListLiveData.getValue();
                List<Task> data = resource.getData();
                resource.onFailure(msg, data);
                runOnMainThread(toDoTaskListLiveData, resource);
            }
        }, context);
    }

    /**
     * 更新用户的经验值
     * @param experience
     * @param context
     */
    public void updateExperience(double experience, Context context){
        HomeRepository.getHomeRepository().updateExperience(experience, context, new CallBack() {
            @Override
            public void onSuccess(Object data) {
                Result<User> result = (Result) data;
                updateRunOnMainThread(isUpdateLiveData, Resource.success(result));
            }

            @Override
            public void onFailure(String msg) {
                updateRunOnMainThread(isUpdateLiveData, Resource.failure(msg, null));
            }
        });
    }

    /**
     * 将LiveData的赋值放在主线程上
     * @param taskListLiveData
     * @param data
     */
    private void runOnMainThread(MutableLiveData<Resource<List<Task>>> taskListLiveData, Resource<List<Task>> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            taskListLiveData.setValue(data);
        else
            //当前线程不在主线程
            taskListLiveData.postValue(data);
    }

    /**
     * 将isUpdateLiveData的赋值放在主线程上
     * @param isUpdateLiveData
     * @param data
     */
    private void updateRunOnMainThread(MutableLiveData<Resource<Result>> isUpdateLiveData, Resource<Result> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            isUpdateLiveData.setValue(data);
        else
            //当前线程不在主线程
            isUpdateLiveData.postValue(data);
    }

}